package com.pfdu.simplescheduler.event;

import com.pfdu.simplescheduler.node.NodeContext;
import com.pfdu.simplescheduler.schedule.TaskCleaner;
import com.pfdu.simplescheduler.schedule.TaskResetSentinel;
import com.pfdu.simplescheduler.schedule.TaskScheduler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ApplicationClosedListener implements ApplicationListener<ContextClosedEvent> {
    @Autowired
    private TaskScheduler taskScheduler;
    @Autowired
    private TaskResetSentinel taskResetSentinel;
    @Autowired
    private NodeContext nodeContext;
    @Autowired
    private TaskCleaner taskCleaner;

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        // spring、mvc 各一次？
        log.info("应用关闭信息--" + contextClosedEvent.getSource().toString());
        taskScheduler.stop();
        taskResetSentinel.stop();
        taskCleaner.stop();
        nodeContext.destroy();
    }
}
